unit clscontrolepadrao;

interface

uses Dm, dbtables, grids, clsempresa, biblio, Dialogs;

type
  TControlePadrao = class(TObject)
  public
    cod_emp: string;
    cod_ref: string;
    function Recuperar(cod_emp: string): boolean;
    function Recuperar1(cod_emp: string): boolean;
    function RecuperarRef(cod_ref: string): boolean;
    function RecuperarReferencia(cod_emp: string): boolean;
    procedure Gravar(cod_emp, cod_ref: string);
    procedure Excluir(cod_emp: string);
    procedure Consultar(var sg: TStringGrid; cod_emp: string);
    procedure ConsultarQuente(var Empr: string);
    function RecuperarPrimeiro: string;
  end;

implementation

procedure TControlePadrao.Gravar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(DmInt);
  tq.databasename := dmint.Db.DatabaseName;
  tq.active := false;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into ControlePadrao values (:a,:b)');
  tq.Params[0].asstring := strzero(cod_emp, 3);
  tq.params[1].asstring := strzero(cod_ref, 3);
  tq.execsql;
  tq.close;
  tq.Free;
end;

function TControlePadrao.Recuperar;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from ControlePadrao where cod_emp=:a');
  tq.Params[0].asstring := strzero(cod_emp, 3);

  tq.open;
  if not tq.eof then begin
    result := true;
    cod_ref := tq.fieldbyname('cod_ref').asstring;
  end else begin
    result := false;
    cod_ref := '';
  end;

  tq.Close;
  tq.Free;
end;

function TControlePadrao.RecuperarReferencia;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from ControlePadrao where cod_ref=:a');
  tq.Params[0].asstring := strzero(cod_emp, 3);
  tq.open;
  if not tq.eof then
    result := true
  else
    result := false;
  tq.Close;
  tq.Free;
end;

procedure TControlePadrao.Excluir;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Delete from ControlePadrao where cod_emp=:a');
  tq.Params[0].asstring := strzero(cod_emp, 3);
  tq.execsql;
  tq.close;
  tq.Free;
end;

procedure TControlePadrao.Consultar;
var
  tq: TQuery;
  i: integer;
  empresa: Tempresa;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from ControlePadrao where cod_emp=:a');
  tq.Params[0].asstring := strzero(cod_emp, 3);
  tq.open;
  i := 0;
  sg.RowCount := 2;
  empresa := Tempresa.Create;
  while not tq.eof do begin
    if i > 0 then sg.RowCount := sg.RowCount + 1;
    sg.cells[0, i + 1] := tq.fieldbyname('cod_ref').asstring;
    empresa.Recuperar(tq.fieldbyname('cod_ref').asstring);
    sg.cells[1, i + 1] := empresa.emp_razao;
    i := i + 1;
    tq.next;
  end;
  empresa.free;
  tq.close;
  tq.free;
end;

function TControlePadrao.RecuperarRef(cod_ref: string): boolean;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from ControlePadrao where cod_ref=:a');
  tq.Params[0].asstring := strzero(cod_ref, 3);
  tq.open;
  if not tq.eof then begin
    result := true;
    cod_emp := tq.fieldbyname('cod_emp').asstring;
  end else
    result := false;
  tq.Close;
  tq.Free;
end;

procedure TControlePadrao.ConsultarQuente(var Empr: string);
var
  tq: TQuery;
  xEmpr: string;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := dmint.db.databasename;
  tq.active := False;
  tq.sql.Clear;
  tq.params.clear;
  tq.sql.add('Select * from ControlePadrao');
  tq.Open;
  xEmpr := '';
  while not tq.Eof do begin
    if Recuperar(tq.FieldByName('Cod_Emp').AsString) then begin
      if xempr = '' then
        xempr := char(39) + copy(tq.FieldByName('Cod_Emp').AsString, 1, 3) + char(39)
      else
        xempr := xempr + ',' + char(39) + copy(tq.FieldByName('Cod_Emp').AsString, 1, 3) + char(39);
    end;
    tq.next;
  end;
  tq.Close;
  tq.Free;
  Empr := xEmpr;
end;

function TControlePadrao.Recuperar1(cod_emp: string): boolean;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select * from ControlePadrao where cod_emp=:a');
  tq.Params[0].asstring := strzero(cod_emp, 3);
  tq.open;
  if not tq.eof then begin
    result := true;
    cod_ref := tq.fieldbyname('cod_ref').asstring;
  end else begin
    cod_ref := '';
    result := false;
  end;
  tq.Close;
  tq.Free;
end;

function TControlePadrao.RecuperarPrimeiro: string;
var
  tq: TQuery;
begin
  tq := TQuery.Create(dmint);
  tq.databasename := DmInt.Db.DatabaseName;
  tq.active := False;
  tq.sql.clear;
  tq.sql.add('Select min(Cod_Emp) as Empr from ControlePadrao ');
  tq.Open;
  Result := tq.FieldByName('Empr').AsString;
  tq.Close;
  tq.Free;
end;

end.
